"""
将视频压缩至指定尺寸
"""
import os
import cv2
import time


if __name__ == '__main__':
    src_dir = 'raw'
    tar_dir = 'videos'
    time_mark = time.strftime("%H%M%S", time.localtime(time.time()))
    file_list = os.listdir(src_dir)
    print(file_list)
    for filename in file_list:
        index = 0
        vid_list = os.listdir(os.path.join(src_dir, filename))
        print(vid_list)
        for vid_name in vid_list:
            video_capture = cv2.VideoCapture(os.path.join(src_dir, filename, vid_name))
            output_name = tar_dir + '/' + filename + '/' + time_mark + '_' + str(index) + '.mp4'
            output_video = cv2.VideoWriter(output_name, cv2.VideoWriter_fourcc(*'XVID'), 30.0, (224, 224), True)
            while True:
                ret, frame = video_capture.read()
                # 判断视频流中的帧是否存在
                if frame is None:
                    break
                frame = cv2.resize(frame, (224, 224))
                output_video.write(frame)
            index += 1
